﻿using AutoMapper;
using MediatR;
using Traceability.APP.Application.Command.Traceablitys;
using Traceability.Damoin;
using Traceability.ErrorCode;
using Traceability.Infaseture;

namespace Traceability.APP.Application.CommandHadnLer.Traceablitys
{
    public class AddTraceablityCommandHandLer : IRequestHandler<AddTraceablityCommand, APIResult<int>>
    {

        private readonly IBaseRepostiy<Traceablity> _traceablity;
        private readonly IMapper mapper;

        public AddTraceablityCommandHandLer(IBaseRepostiy<Traceablity> traceablity, IMapper mapper)
        {
            _traceablity = traceablity;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(AddTraceablityCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            var list = mapper.Map<Traceablity>(request);

            var res = await _traceablity.AddAsync(list);
            result.Code = ResultCode.Ok;
            result.Message = "商品信息添加成功";
            result.Data = res;
            return await Task.FromResult(result);
        }
    }
}
